package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DoctorSendMessagePatientDTO;
import com.pshcp.domain.model.DoctorSendMessagePatient;
import com.pshcp.mapper.DoctorSendMessagePatientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 医生群发消息患者表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorSendMessagePatientService {

    @Autowired
    private DoctorSendMessagePatientMapper doctorSendMessagePatientMapper;

    public void batchSaveDoctorSendMessagePatient(List<DoctorSendMessagePatient> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        Map<String, List<DoctorSendMessagePatient>> infoMap = new HashMap<>();
        infoMap.put("infoMap", list);
        doctorSendMessagePatientMapper.batchSaveDoctorSendMessagePatient(infoMap);
    }

    public PageList<DoctorSendMessagePatient> list(DoctorSendMessagePatientDTO doctorSendMessagePatientDTO) {
        if (doctorSendMessagePatientDTO.getPageNum() != null && doctorSendMessagePatientDTO.getPageSize() != null) {
            PageHelper.startPage(doctorSendMessagePatientDTO.getPageNum(), doctorSendMessagePatientDTO.getPageSize());
        }
        Example example = new Example(DoctorSendMessagePatient.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorSendMessagePatientDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorSendMessagePatientDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorSendMessagePatient> list = doctorSendMessagePatientMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorSendMessagePatient> select(DoctorSendMessagePatient doctorSendMessagePatient) {
        return doctorSendMessagePatientMapper.select(doctorSendMessagePatient);
    }

    public int insert(DoctorSendMessagePatient doctorSendMessagePatient) {
        doctorSendMessagePatient.setId(SystemUtil.getId());
        doctorSendMessagePatient.setStatus(CommonStatusEnum.INIT.getCode());
        doctorSendMessagePatient.setCreatedTime(new Date());
        return doctorSendMessagePatientMapper.insert(doctorSendMessagePatient);
    }

    public int update(DoctorSendMessagePatient doctorSendMessagePatient) {
        doctorSendMessagePatient.setUpdateTime(new Date());
        return doctorSendMessagePatientMapper.updateByPrimaryKeySelective(doctorSendMessagePatient);
    }

    public DoctorSendMessagePatient findById(Long id) {
        return doctorSendMessagePatientMapper.selectByPrimaryKey(id);
    }

    public List<DoctorSendMessagePatient> findByIds(List<Long> ids) {
        Example example = new Example(DoctorSendMessagePatient.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorSendMessagePatientMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorSendMessagePatient.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorSendMessagePatient doctorSendMessagePatient = new DoctorSendMessagePatient();
        doctorSendMessagePatient.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorSendMessagePatientMapper.updateByExampleSelective(doctorSendMessagePatient, example);
    }

}
